import originJsonp from "jsonp"

const buildUrl = (url, data) => {
    // console.log(url)
    // console.log(data)
    let params = []
    for (let k in data) {
        params.push(`${k}=${data[k]}`)
    }
    let param = params.join('&')
    if (url.indexOf('?') === -1) {
        url += '?' + param
    } else {
        url += '&' + param
    }
    return url
}

const jsonp = (url, data, options) => {
    return new Promise((resolve, reject) => {
        originJsonp(buildUrl(url, data), options, (err, data) => {
            if (!err) {
                resolve(data)
            } else {
                reject(err)
            }
        })
    })
}

export default jsonp